<?php

namespace App\Evaluation\Controllers;


use App\Evaluation\Actions\Post\PatentAcceptPost;
use App\Evaluation\Actions\Post\PatentRetrievedPost;
use App\Http\Controllers\Controller;
use App\Repositories\PatentApplyRepository;
use App\Services\PatentApplyService;
use App\Services\SettingService;
use Carbon\Carbon;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class PatentApplyController extends Controller
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '专利检索';
    protected $service;
    public $repository;

    public function __construct(PatentApplyService $service, PatentApplyRepository $repository)
    {
        $this->service = $service;
        $this->repository = $repository;
    }

    public function retrieval(Content $content): Content
    {
        $grid = $this->grid();
        $grid->model()->where('review_status', 0)->whereNull('retrieval_report');
        return $content
            ->title('专利申请前检索')
            ->description('待检索')
            ->body($grid);
    }

    public function retrieved(Content $content): Content
    {
        $grid = $this->grid(1);
        $grid->model()
            //->whereNotNull('retrieval_report')
            ->whereIn('review_status', [0, 1]);
        $grid->column('phone', __('联系电话'));
        $grid->column('review_status', __('审批状态'))
            ->using(['审批中', '审批通过'])
            ->label(['warning', 'success'])
            ->filter(['审批中', '审批通过']);
        return $content
            ->title('已检索')
            ->body($grid);
    }

    public function patent(Content $content)
    {
        return $content->title('专利清单')
            ->body($this->patentGrid());
    }

    protected function patentGrid()
    {
        $user = Admin::user();
        $grid = $this->repository->grid();
        $grid->model()->where('agency_id', $user['id']);
            //->whereNotNull('patent_sn');
        $grid->column('patent_sn', __('Patent sn'))->filter('like');
        $grid->column('patent_name', __('专利名称'))->filter('like');
        $grid->column('patent_type_id', __('专利类型'))->using(patent_type_select_option())->filter(patent_type_select_option());
        $grid->column('apply_date', __('Apply date'));
        $grid->column('agency.company', __('代理机构'));
        $grid->actions(function (\Encore\Admin\Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableEdit();
            $actions->disableDelete();
        });
        $grid->disableCreateButton();
        $grid->batchActions(function (Grid\Tools\BatchActions $actions) {
            $actions->disableDeleteAndHodeSelectAll();
        });
        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter();
        });
        return $grid;
    }

    protected function grid($type = 0): Grid
    {
        $user = Admin::user();
        $grid = $this->repository->grid();
        $grid->model()->where('agency_id', $user['id'])
            ->where('dept_review_status', 1)
            ->with(['remarks.user', 'assesses.expert', 'experts']);
        if (!$type) {
            $grid->column('retrieval_report_template', '查新检索报告模板')->display(function () {
                return SettingService::patentRetrievalReportTemplateATag();
            });
        }
        $grid->column('patent_name', __('专利名称'));
        $grid->column('patent_type_id', __('专利类型'))->using(patent_type_select_option())->filter(patent_type_select_option());
        $grid->column('charge_person', __('第一发明人'));
        $grid->column('department.name', __('dept_name'));
        $grid->column('retrieval_report', '检索报告')->display(function ($value) {
            return $value ? '查看' : '';
        })->link(function ($model) {
            return file_custom_preview_url($model['retrieval_report'], '(检索报告)' . $model['patent_name'], 'show');
        });
//        $grid->column('_table', '评估表')->display(function () {
//            return '下载';
//        })->link(function ($model) {
//            return admin_route('files.patent-apply.table', [$model['id'], 'token' => $model->token()]);
//        });
        $grid->column('file', '交底书')->display(function ($value) {
            return $value ? '下载' : '';
        })->link(function ($model) {
            return file_custom_preview_url($model['file'], '(交底书)' . $model['patent_name']);
        });
        $grid->column('file_time', '上传时间')->display(function ($value) {
            return $value ? Carbon::parse($value)->toDateString() : '';
        });
        $grid->batchActions(function (Grid\Tools\BatchActions $actions) {
            $actions->disableDeleteAndHodeSelectAll();
        });
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableFilter();
        $grid->actions(function (Grid\Displayers\Actions $actions) use ($type) {
            $actions->disableView();
            $actions->disableDelete();
            $actions->disableEdit();
            $model = $actions->row;
            if (!$model->review_status && $model->dept_review_status == 1) {
                $actions->prepend(new PatentRetrievedPost($actions));
            }
//            if ($type == 1 && $model->review_status == 1 && !$model->patent) {
//                $actions->prepend(new PatentAcceptPost($actions));
//            }
        });
        $this->service->quickSearch($grid);
        return $grid;
    }

}
